﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Input;
using ESRI.ArcGIS;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Controls;
using GISDeveloping_test2;
using ESRI.ArcGIS.SystemUI;
using ESRI.ArcGIS.Geodatabase;

namespace GISDeveloping_test4
{
    public partial class Form1 : Form
    {
        OpenDocument openDocument;
        SaveDocument saveDocument;
        ZoomIn zoomIn;
        ZoomOut zoomOut;
        FullExtent fullExtent;
        LoadLayer loadLayer;
        private IToolbarMenu m_menuLayer;
        private IMapControl3 m_mapControl;
        private ITOCControl2 m_tocControl;
        private IFeatureWorkspace m_Workspace;
        ILayer layer = null;
        CmdExpQuery CmdExpQuery1;
        MyIdentifyTool myIdentifyTool;
        public IFeatureLayer EditingLayer;
        IWorkspaceEdit _workspaceEdit;



        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            openDocument = new OpenDocument(axMapControl1);
            saveDocument = new SaveDocument(axMapControl1);
            zoomIn = new ZoomIn(axMapControl1);
            zoomOut = new ZoomOut(axMapControl1);
            fullExtent = new FullExtent(axMapControl1);
            fullExtent.OnCreate(axMapControl1.Object);
            loadLayer = new LoadLayer(axMapControl1,axTOCControl1);
            CmdExpQuery1 = new CmdExpQuery();
            myIdentifyTool = new MyIdentifyTool();


            //对于设置hook很重要
            m_tocControl = (ITOCControl2)axTOCControl1.Object;
            m_mapControl = (IMapControl3)axMapControl1.Object;
            m_menuLayer = new ToolbarMenuClass();
            m_menuLayer.AddItem(new RemoveLayer(), -1, 0, false, ESRI.ArcGIS.SystemUI.esriCommandStyles.esriCommandStyleTextOnly);
            m_menuLayer.AddItem(new ZoomToLayer(), -1, 0, true, ESRI.ArcGIS.SystemUI.esriCommandStyles.esriCommandStyleTextOnly);
            m_menuLayer.AddItem(new ShowAttribute(), -1, 0, true, ESRI.ArcGIS.SystemUI.esriCommandStyles.esriCommandStyleTextOnly);
            m_menuLayer.AddItem(new CmdOpenLayerRender(), -1, 0, true, ESRI.ArcGIS.SystemUI.esriCommandStyles.esriCommandStyleTextOnly);
            m_menuLayer.SetHook(m_mapControl);
            //对于设置hook很重要


            CmdExpQuery1.OnCreate(axMapControl1.Object);//创建axMapControl的钩子，实现与CmdExpQuery1的连接
            myIdentifyTool.OnCreate(axMapControl1.Object);


            axToolbarControl1.AddItem(openDocument, -1, -1, true, 0,
                ESRI.ArcGIS.SystemUI.esriCommandStyles.esriCommandStyleIconAndText);
            axToolbarControl1.AddItem(saveDocument, -1, -1, true, 0,
                ESRI.ArcGIS.SystemUI.esriCommandStyles.esriCommandStyleIconAndText);
            axToolbarControl1.AddItem(zoomIn, -1, -1, true, 0,
                ESRI.ArcGIS.SystemUI.esriCommandStyles.esriCommandStyleIconAndText);
            axToolbarControl1.AddItem(zoomOut, -1, -1, true, 0,
               ESRI.ArcGIS.SystemUI.esriCommandStyles.esriCommandStyleIconAndText);
            axToolbarControl1.AddItem(fullExtent, -1, -1, true, 0,
               ESRI.ArcGIS.SystemUI.esriCommandStyles.esriCommandStyleIconOnly);
            axToolbarControl1.AddItem(loadLayer, -1, -1, true, 0,
               ESRI.ArcGIS.SystemUI.esriCommandStyles.esriCommandStyleIconOnly);
            axToolbarControl1.AddItem(new CreateGDBCommand(m_Workspace), -1, -1, false, 0,
               esriCommandStyles.esriCommandStyleIconOnly);
            axToolbarControl1.AddItem(new CreateFeatureCommand(axMapControl1, m_Workspace), -1, -1, false, 0,
  esriCommandStyles.esriCommandStyleIconOnly);
            axToolbarControl1.AddItem(new StartEditCommand(axMapControl1, EditingLayer, _workspaceEdit, this), -1, -1, false, 0,
              esriCommandStyles.esriCommandStyleIconOnly);
            axToolbarControl1.AddItem(new EditTool(_workspaceEdit, EditingLayer, axMapControl1), -1, -1, false, 0,
                    esriCommandStyles.esriCommandStyleIconOnly);
            axToolbarControl1.AddItem(new StopEditCommand(axMapControl1), -1, -1, false, 0,
              esriCommandStyles.esriCommandStyleIconOnly);
            axToolbarControl1.AddItem(CmdExpQuery1, -1, -1, true, 0,
               ESRI.ArcGIS.SystemUI.esriCommandStyles.esriCommandStyleIconOnly);
            axToolbarControl1.AddItem(myIdentifyTool, -1, -1, false, 0,
                ESRI.ArcGIS.SystemUI.esriCommandStyles.esriCommandStyleIconOnly);





        }

        private void menuNew_Click(object sender, EventArgs e)
        {
            //ICommand cmdNew = new OpenDocument();
            //cmdNew.OnCreate(axMapControl1.Object);
            //cmdNew.OnClick();
        }


        /// <summary>
        /// 当鼠标中键按下时拖动地图
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void axMapControl1_OnMouseDown(object sender, ESRI.ArcGIS.Controls.IMapControlEvents2_OnMouseDownEvent e)
        {
            if (e.button==4)
            {
                this.Cursor = Cursors.Hand;
                axMapControl1.Pan();
                
            }
        }

        private void axTOCControl1_OnEndLabelEdit(object sender, ESRI.ArcGIS.Controls.ITOCControlEvents_OnEndLabelEditEvent e)
        {
            if (e.newLabel.Trim() == "")
            {
                e.canEdit = false;
                MessageBox.Show("无法将图层名修改为空", "非法的命名", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void axTOCControl1_OnMouseDown(object sender, ESRI.ArcGIS.Controls.ITOCControlEvents_OnMouseDownEvent e)
        {
            esriTOCControlItem item = esriTOCControlItem.esriTOCControlItemNone;
            IBasicMap map = null;
            object other = null;
            object index = null;
            //右击图层名
            if (e.button == 2)
            {
                //定义被选中的对象
                m_tocControl.HitTest(e.x, e.y, ref item, ref map, ref layer, ref other, ref index);
                //确保对象被选中
                if (item == esriTOCControlItem.esriTOCControlItemMap)
                    m_tocControl.SelectItem(map, null);
                else
                    m_tocControl.SelectItem(layer, null);
                m_mapControl.CustomProperty = layer;
                m_menuLayer.PopupMenu(e.x, e.y, axTOCControl1.hWnd);
            }
        }


        /// <summary>
        /// 当选择集变化时让toolStrip显示当前选中要素的数量
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void axMapControl1_OnSelectionChanged(object sender, EventArgs e)
        {
            toolStripStatusLabel1.Text = "当前已选中" + axMapControl1.Map.SelectionCount.ToString() + "个要素";
        }

        private void axMapControl1_OnViewRefreshed(object sender, IMapControlEvents2_OnViewRefreshedEvent e)
        {
            axTOCControl1.Refresh();
        }

        private void axTOCControl1_OnDoubleClick(object sender, ITOCControlEvents_OnDoubleClickEvent e)
        {
            esriTOCControlItem itemType = esriTOCControlItem.esriTOCControlItemNone;
            IBasicMap basicMap = null;
            ILayer layer = null;
            object unk = null;
            object data = null;

            //HitTest(鼠标点击的X坐标，鼠标点击的Y坐标，esriTOCControlItem枚举常量，绑定MapControl的IBasicMap接口
            //被点击的图层，TOCControl的LegendGroup对象，LegendClass在LegendGroup中的Index)
            axTOCControl1.HitTest(e.x, e.y, ref itemType, ref basicMap, ref layer, ref unk, ref data);
            if (e.button == 1)
            {
                //限定只有在双击符号时才触发
                if (itemType == esriTOCControlItem.esriTOCControlItemLegendClass)
                {
                    //取得图例
                    ILegendClass pLegendClass = ((ILegendGroup)unk).get_Class((int)data);
                    //创建符号选择器SymbolSelector实例
                    frmSymbolSelector SymbolSelectorFrm = new frmSymbolSelector(pLegendClass, layer);
                    if (SymbolSelectorFrm.ShowDialog() == DialogResult.OK)
                    {
                        axMapControl1.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeography, null, null);
                        pLegendClass.Symbol = SymbolSelectorFrm.pSymbol;
                        axMapControl1.Refresh();
                        axTOCControl1.Update();
                    }
                }
            }
        }
    }
}
